package org.piaohao.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.piaohao.model.DepOrgTree;
import org.piaohao.model.Department;
import org.piaohao.model.OrgTree;
import org.piaohao.util.string.JsonUtil;
import org.piaohao.util.string.UUIDGenerator;

/**
 * Created on : 2013-5-4, 17:23:35
 *
 * @author piaohao
 */
public class DepController extends Controller {

    public static final String moduleName = "部门";

    public void add() {
        try {
            Department dep = getModel(Department.class);
            String orgTreeId = getPara("p_id");
            Record oot = Db.findFirst("select * from t_org_org_tree where fk_org_tree_id=?", orgTreeId);
            String orgId = oot.getStr("fk_org_id");
            dep.set("org_id", orgId);
            String id1 = UUIDGenerator.getUUID();
            if (dep.getStr("id") == null || dep.getStr("id").trim().equals("")) {
                dep.set("id", id1);
                dep.save();
            }
            OrgTree ot = new OrgTree();
            String id2 = UUIDGenerator.getUUID();
            ot.set("id", id2);
            ot.set("p_id", orgTreeId);
            ot.set("text", dep.getStr("name"));
            ot.set("leaf", false);
            ot.set("type", "部门");
            ot.set("iconCls", "department");
            ot.save();

            DepOrgTree dot2 = new DepOrgTree();
            String id3 = UUIDGenerator.getUUID();
            dot2.set("id", id3);
            dot2.set("fk_dep_id", id1);
            dot2.set("fk_org_tree_id", id2);
            dot2.save();
            Map extMap = new HashMap();
            extMap.put("id", id2);
            renderJson(JsonUtil.printSuccess(true, "成功", extMap));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void update() {
        try {
            Department dep = getModel(Department.class);
            String depId = Db.queryStr("select fk_dep_id from t_dep_org_tree where fk_org_tree_id=?", getPara("org_tree_id"));
            dep.set("id", depId);
            dep.update();
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void delete() {
        try {
            String orgTreeId = getPara("id");
            String depId = Db.queryStr("select fk_dep_id from t_dep_org_tree where fk_org_tree_id=?", orgTreeId);
            String orgId = Db.queryStr("select org_id from t_department where id=?", depId);
            Department.dao.deleteById(depId);
            OrgTree.dao.deleteById(orgTreeId);
            Db.update("delete from t_position where dep_id=?", depId);
            Db.update("delete from t_dep_org_tree where fk_org_tree_id=?", orgTreeId);
            Db.update("update t_employee set dep_id=? ,pos_id=? where org_id=?", null, null, orgId);
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void getAll() {
        try {
            List<Department> deps = Department.dao.find("select name,id from t_department where org_id=?", getPara(0));
            renderJson(deps);
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }

    public void showDetail() {
        try {
            String id = getPara(0);
            Record dep = Db.findFirst("select d.* from t_department d left join t_dep_org_tree dot on d.id=dot.fk_dep_id where dot.fk_org_tree_id=?", id);
            setAttr("dep", dep);
            render("/pages/organization/depDetail.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }
}
